unit frmTodoListUnit;

{$mode objfpc}{$H+}

{
The MIT License (MIT)

Copyright (c) 2012 Gustavo Carreno <guscarreno@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, ExtCtrls, StdCtrls, ActnList,
  ComCtrls, Dialogs, VirtualTrees;

type

  { TfrmTodoList }

  TfrmTodoList = class(TFrame)
    actTodoCancel: TAction;
    actTodoOk: TAction;
    actTLTodoDown: TAction;
    actTLTodoUp: TAction;
    actTLTodoDelete: TAction;
    actTLTodoEdit: TAction;
    actTLTodoAdd: TAction;
    actTLFileSave: TAction;
    actlTodoList: TActionList;
    btnTodoOk: TButton;
    btnTodoCancel: TButton;
    cbTodoDone: TCheckBox;
    edtTodoTitle: TEdit;
    gbTodoDetails: TGroupBox;
    imTodoList: TImageList;
    lblTodoDescription: TLabel;
    lblTodoTitle: TLabel;
    memTodoDescription: TMemo;
    panTodoButtons: TPanel;
    panTodoListTitle: TPanel;
    Splitter1: TSplitter;
    tbTodoList: TToolBar;
    tbbSave: TToolButton;
    ToolButton1: TToolButton;
    tbbAdd: TToolButton;
    tbbEdit: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    tbbDelete: TToolButton;
    ToolButton4: TToolButton;
    tbbUp: TToolButton;
    ToolButton5: TToolButton;
    tbbDown: TToolButton;
    vstTodoList: TVirtualStringTree;
    procedure actTLFileSaveExecute(Sender: TObject);
    procedure actTLTodoAddExecute(Sender: TObject);
    procedure actTLTodoDeleteExecute(Sender: TObject);
    procedure actTLTodoDownExecute(Sender: TObject);
    procedure actTLTodoEditExecute(Sender: TObject);
    procedure actTLTodoUpExecute(Sender: TObject);
    procedure actTodoCancelExecute(Sender: TObject);
    procedure actTodoOkExecute(Sender: TObject);
    procedure vstTodoListChange(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure vstTodoListChecked(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure vstTodoListGetNodeDataSize(Sender: TBaseVirtualTree;
      var NodeDataSize: Integer);
    procedure vstTodoListGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
    procedure vstTodoListLoadNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Stream: TStream);
    procedure vstTodoListSaveNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Stream: TStream);
  private
    { private declarations }
    FHasChanged: Boolean;
    FEditing: Boolean;
    FAdding: Boolean;
  public
    { public declarations }
    procedure Initialize;
  end; 

implementation

uses
  CEOResourcesUnit;

type
  PTodoListItem = ^TTodoListItem;
  TTodoListItem = record
    Done: Boolean;
    Title: String;
    Desc: String;
  end;

{$R *.lfm}

{ TfrmTodoList }

procedure TfrmTodoList.actTLFileSaveExecute(Sender: TObject);
begin
  vstTodoList.SaveToFile(envAppTodoListFile);
  FHasChanged:= False;
  actTLFileSave.Enabled:= False;
end;

procedure TfrmTodoList.actTLTodoAddExecute(Sender: TObject);
var
  TodoNode: PVirtualNode;
  TodoData: PTodoListItem;
begin
  TodoNode:= vstTodoList.AddChild(vstTodoList.RootNode);
  TodoNode^.CheckType:= ctCheckBox;
  TodoData:= vstTodoList.GetNodeData(TodoNode);
  if Assigned(TodoData) then
  begin
    FAdding:= True;
    gbTodoDetails.Enabled:= True;
    TodoData^.Title:='Please fill in the title.';
    TodoData^.Desc:='Please add a Description.';
    TodoData^.Done:= False;
    vstTodoList.Selected[TodoNode]:= True;
    vstTodoList.FocusedNode:= TodoNode;
    vstTodoList.Enabled:= False;
    actTLFileSave.Enabled:= True;
  end;
  vstTodoList.Refresh;
end;

procedure TfrmTodoList.actTLTodoDeleteExecute(Sender: TObject);
var
  TodoNode: PVirtualNode;
  mRes: Integer;
begin
  TodoNode:=vstTodoList.FocusedNode;
  if Assigned(TodoNode) then
  begin
    mRes:= MessageDlg('Are you sure?', mtConfirmation, [mbYes, mbNo], 0);
    if mRes = mrYes then
    begin
      vstTodoList.DeleteNode(TodoNode);
      actTLFileSave.Enabled:= True;
      FHasChanged:= True;
      if vstTodoList.RootNodeCount = 0 then
      begin
        actTLTodoDelete.Enabled:= False;
        actTLTodoEdit.Enabled:= False;
        actTLTodoUp.Enabled:= False;
        actTLTodoDown.Enabled:= False;
      end
      else
      begin
        TodoNode:= vstTodoList.GetFirstChild(vstTodoList.RootNode);
        vstTodoList.Selected[TodoNode]:= True;
      end;
    end;
  end;
  vstTodoList.Refresh;
end;

procedure TfrmTodoList.actTLTodoDownExecute(Sender: TObject);
var
  TodoNode,
  TodoNodeDown: PVirtualNode;
begin
  TodoNode:= vstTodoList.FocusedNode;
  TodoNodeDown:= vstTodoList.GetNextSibling(TodoNode);
  if vstTodoList.AbsoluteIndex(TodoNodeDown) = (vstTodoList.RootNodeCount +1) then
    actTLTodoDown.Enabled:= False;
  vstTodoList.MoveTo(TodoNode, TodoNodeDown, amInsertAfter,False);
end;

procedure TfrmTodoList.actTLTodoEditExecute(Sender: TObject);
var
  TodoNode: PVirtualNode;
begin
  TodoNode:= vstTodoList.FocusedNode;
  if Assigned(TodoNode) then
  begin
    FEditing:= True;
    gbTodoDetails.Enabled:= True;
    vstTodoList.Enabled:= False;
  end;
end;

procedure TfrmTodoList.actTLTodoUpExecute(Sender: TObject);
var
  TodoNode,
  TodoNodeUp: PVirtualNode;
begin
  TodoNode:= vstTodoList.FocusedNode;
  TodoNodeUp:= vstTodoList.GetPreviousSibling(TodoNode);
  if vstTodoList.AbsoluteIndex(TodoNodeUp) = 0 then
    actTLTodoUp.Enabled:= False;
  vstTodoList.MoveTo(TodoNode, TodoNodeUp, amInsertBefore, False);
end;

procedure TfrmTodoList.actTodoCancelExecute(Sender: TObject);
begin
  if FEditing then
    FEditing:= False;
  if FAdding then
  begin
    FAdding:= False;
    vstTodoList.DeleteNode(vstTodoList.FocusedNode);
  end;
  gbTodoDetails.Enabled:= False;
  vstTodoList.Enabled:= True;
end;

procedure TfrmTodoList.actTodoOkExecute(Sender: TObject);
var
  TodoData: PTodoListItem;
  TodoNode: PVirtualNode;
begin
  if FEditing then
    FEditing:= False;
  if FAdding then
    FAdding:= False;
  FHasChanged:= True;
  vstTodoList.BeginUpdate;
  TodoNode := vstTodoList.FocusedNode;
  if Assigned(TodoNode) then
  begin
    TodoData:= vstTodoList.GetNodeData(TodoNode);
    if Assigned(TodoData) then
    begin
      TodoData^.Title:= edtTodoTitle.Text;
      TodoData^.Desc:= memTodoDescription.Lines.Text;
      TodoData^.Done:= cbTodoDone.Checked;
      if TodoData^.Done then
        TodoNode^.CheckState:= csCheckedNormal
      else
        TodoNode^.CheckState:= csUncheckedNormal;
    end;
  end;
  vstTodoList.Enabled:= True;
  vstTodoList.EndUpdate;
  gbTodoDetails.Enabled:= False;
  actTLFileSave.Enabled:= True;
end;

procedure TfrmTodoList.vstTodoListChange(Sender: TBaseVirtualTree;
  Node: PVirtualNode);
var
  TodoData: PTodoListItem;
begin
  TodoData:= Sender.GetNodeData(Node);
  if Assigned(TodoData) then
  begin
    actTLTodoEdit.Enabled:= True;
    actTLTodoDelete.Enabled:= True;
    edtTodoTitle.Text:= TodoData^.Title;
    memTodoDescription.Lines.Text:= TodoData^.Desc;
    cbTodoDone.Checked:= TodoData^.Done;
  end
  else
  begin
    actTLTodoEdit.Enabled:= False;
    actTLTodoDelete.Enabled:= False;
  end;
  actTLTodoUp.Enabled:= True;
  actTLTodoDown.Enabled:= True;
  if vstTodoList.AbsoluteIndex(Node) = 0 then
  begin
    actTLTodoUp.Enabled:= False;
  end;
  if vstTodoList.AbsoluteIndex(Node) = (vstTodoList.RootNodeCount -1) then
  begin
    actTLTodoDown.Enabled:= False;
  end;
end;

procedure TfrmTodoList.vstTodoListChecked(Sender: TBaseVirtualTree;
  Node: PVirtualNode);
var
  TodoData: PTodoListItem;
begin
  TodoData:= Sender.GetNodeData(Node);
  case Node^.CheckState of
    csCheckedNormal:begin
      TodoData^.Done:= True;
    end;
    csUncheckedNormal:begin
      TodoData^.Done:= False;
    end;
  end;
  cbTodoDone.Checked:= TodoData^.Done;
  FHasChanged:= True;
  actTLFileSave.Enabled:= True;
end;

procedure TfrmTodoList.vstTodoListGetNodeDataSize(Sender: TBaseVirtualTree;
  var NodeDataSize: Integer);
begin
  NodeDataSize:= SizeOf(TTodoListItem);
end;

procedure TfrmTodoList.vstTodoListGetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: String);
var
  TodoData: PTodoListItem;
begin
  TodoData:= Sender.GetNodeData(Node);
  if Assigned(TodoData) then
  begin
    case Column of
      -1,0:begin
        CellText:= '';
        if TodoData^.Done then
          Node^.CheckState:= csCheckedNormal
        else
          Node^.CheckState:= csUncheckedNormal;
      end;
      1:begin
        if Length(TodoData^.Title)>0 then
          CellText:=TodoData^.Title
        else
          CellText:='Example title of todo item';
      end;
      2:begin
        CellText:='Col3 Text';
      end;
    end;
  end;
end;

procedure TfrmTodoList.vstTodoListLoadNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Stream: TStream);
var
  TodoData: PTodoListItem;
  Len: Integer;
begin
  TodoData:= Sender.GetNodeData(Node);

  Stream.Read(Len, SizeOf(Len));
  Stream.Read(TodoData^.Done, Len);

  Stream.Read(Len, SizeOf(Len));
  SetLength(TodoData^.Title, Len);
  Stream.Read(PChar(TodoData^.Title)^, Len);

  Stream.Read(Len, SizeOf(Len));
  SetLength(TodoData^.Desc, Len);
  Stream.Read(PChar(TodoData^.Desc)^, Len);
end;

procedure TfrmTodoList.vstTodoListSaveNode(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Stream: TStream);
var
  TodoData: PTodoListItem;
  Len: Integer;
begin
  TodoData:= Sender.GetNodeData(Node);

  Len:= SizeOf(TodoData^.Done);
  Stream.Write(Len, SizeOf(Len));
  Stream.Write(TodoData^.Done, Len);

  Len:= Length(TodoData^.Title);
  Stream.Write(Len, SizeOf(Len));
  Stream.Write(PChar(TodoData^.Title)^, Len);

  Len:= Length(TodoData^.Desc);
  Stream.Write(Len, SizeOf(Len));
  Stream.Write(PChar(TodoData^.Desc)^, Len);
end;

procedure TfrmTodoList.Initialize;
var
  TodoNode: PVirtualNode;
begin
  gbTodoDetails.Height:= 90;
  if FileExists(envAppTodoListFile) then
  begin
    vstTodoList.LoadFromFile(envAppTodoListFile);
  end;
  if vstTodoList.RootNodeCount = 0 then
  begin
    actTLTodoDelete.Enabled:= False;
    actTLTodoEdit.Enabled:= False;
    actTLTodoUp.Enabled:= False;
    actTLTodoDown.Enabled:= False;
  end
  else
  begin
    TodoNode:= vstTodoList.GetFirstChild(vstTodoList.RootNode);
    vstTodoList.Selected[TodoNode]:= True;
    vstTodoList.FocusedNode:= TodoNode;
    vstTodoList.Refresh;
  end;
  actTLFileSave.Enabled:= False;
  gbTodoDetails.Enabled:= False;
  FHasChanged:= False;
  FEditing:= False;
  FAdding:= False;

end;

end.

